<?php
namespace App\Api\Controllers\EasyPay;


use App\Models\YinkaStore;
use Illuminate\Support\Facades\Log;
use GuzzleHttp\Client;
class NewBaseController
{
    static $config, $instance, $data;

    const MIAN = 'https://platform.eycard.cn';

//    const MIAN ='https://t-wapi.bhecard.com';

    const PROD1 = '8443';
    const PROD2 = '6111';

    const DOMAIN="https://phoenix.eycard.cn/aclTwo/";

    const CLOSE ='/standard/close';
    const SCAN_PAY = 'standard/scanPay';
    const ORDER_QUERY= 'standard/tradeQuery';

    const REFUND = 'ledger/mposrefund';
    const REFUND_QUERY = 'ledger/mposfindrefund';
    const JS_API_PAY= 'standard/jsapi';



    public static function getInstance ()
    {
        if (! isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function params ($data, $signData, $priKey, $orgId)
    {
        self::$data= $data;
        self::$data['orgId'] = $orgId;
        self::$data['signType'] = 'RSA2';
        self::$data['sign'] = self::createSign(self::signStr($signData), $priKey);
        return $this;
    }


    public static function createSign($data, $priKey, $signType = "RSA2")
    {
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($priKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";

        ($res) or die('您使用的私钥格式错误，请检查RSA私钥配置');

        if ("RSA2" == $signType) {
            openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256);
        } else {
            openssl_sign($data, $sign, $res);
        }

        $sign = base64_encode($sign);
        return $sign;
    }

    public static function signStr ($params)
    {
        $str = '';
        ksort($params);
        $i = 0;
        foreach($params as $k => $v) {
            if ($v === null || $v == null || $v == '') {
                continue;
            }
            if ($i == 0) {
                $str .= "$k" . "=" . "$v";
            } else {
                $str .= "&" . "$k" . "=" . "$v";
            }
            $i++;
        }
        return $str;
    }

    public function build ($url)
    {

        $json = json_encode(self::$data);
        $ch = curl_init();
//        error_log(date('Y-m-d H:i:s')." 【易生支付入参】： ".json_encode([$url, self::$data])."\n\n", 3, "/www/wwwlogs/wft/". 'prod-easy-pay'.date('Ymd').".log");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

        $headers = array();
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Charset: UTF-8';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);
//        error_log(date('Y-m-d H:i:s')." 【易生支付出参】： ".json_encode($result)."\n\n", 3, "/www/wwwlogs/wft/". 'prod-easy-pay'.date('Ymd').".log");
        return json_decode($result, true);
    }

}